import 'dart:ui';

import 'package:date_format/date_format.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:s_mankey/config/resource_mananger.dart';
import 'package:s_mankey/service/smonkey_repository.dart';
import 'package:s_mankey/ui/widget/error_widget.dart';
import 'package:s_mankey/ui/widget/loading_widget.dart';
import 'package:flutter/services.dart';

class InviteDetail extends StatefulWidget {
  final arguments;

  InviteDetail({Key key, this.arguments}) : super(key: key);

  @override
  _Invitedetail createState() => _Invitedetail();
}

class _Invitedetail extends State<InviteDetail> {
  String id; //请求参数
  var datas;
  var list = [
    {'title': '上课时间', 'result': ''},
    {'title': '上课城市', 'result': ''},
    {'title': '课程价格', 'result': ''},
  ];

  @override
  void initState() {
    print('object##${widget.arguments}');
    if (widget.arguments != null) {
      id = widget.arguments.toString();
//      options['city'] = widget.arguments['city'];
//      options['course_type'] = widget.arguments['course_type'];
//      options['member_id'] = widget.arguments['member_id'];
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 1,
        title: Text('邀请详情'),
      ),
      body: Container(
        color: Colors.white,
        child: FutureBuilder(
            future: SmonkeysRepository.fetchCourseInvitationDetail(id: id),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              /// 请求中
              if (snapshot.connectionState != ConnectionState.done) {
                return LoadingWidget();
              }

              /// 请求结束
              if (snapshot.hasError) {
                // 请求失败，显示错误
                return datasErrorWidget(
                  errorTip: snapshot.error.toString(),
                );
              }

              datas = snapshot.data;
              list[0]['result'] = datas['course_date'];
              list[1]['result'] = datas['city'];
              list[2]['result'] = datas['money'];
              // 请求成功，显示数据
              return (datas['code_type'] == 'error')
                  ? Center(
                      child: Text(
                        '邀请码已失效',
                        style: TextStyle(fontSize: AppFontSize.Size30),
                      ),
                    )
                  : Column(
                      children: [
                        //失效
                        Container(
                          height: 90,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Text(
                                '已失效',
                                style: TextStyle(
                                    color: Colors.black,
                                    fontSize: AppFontSize.Size18,
                                    fontWeight: FontWeight.w600),
                              ),
                              Text('当前邀请码已失效'),
                            ],
                          ),
                        ),

                        //姓名
                        Container(
                          color: Color(0xfffafafa),
                          padding: EdgeInsets.only(left: 15),
                          height: 40,
                          child: Row(
                            children: [
                              Text('邀请用户: '),
                              Text(datas['member_name']),
                            ],
                          ),
                        ),

                        //内容
                        Container(
                          margin: EdgeInsets.fromLTRB(15, 0, 0, 0),
                          padding: EdgeInsets.fromLTRB(0, 15, 0, 15),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Image.network(
                                datas['type_photo_link'],
                                width: 82,
                                height: 107,
                                fit: BoxFit.fill,
                              ),
                              Expanded(
                                child: Padding(
                                  padding:
                                      const EdgeInsets.fromLTRB(10, 0, 10, 10),
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      //标题
                                      Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.start,
                                        children: [
                                          Text(datas['course_name']),
                                          IconFonts.rightIcon,
                                        ],
                                      ),

                                      //书内容
                                      Container(
                                        padding:
                                            EdgeInsets.fromLTRB(0, 5, 0, 5),
                                        child: Text(
                                          datas['type_short_desc'],
                                          style: TextStyle(
                                              color: Color(0xff747474),
                                              fontSize: AppFontSize.Size14),
                                          overflow: TextOverflow.ellipsis,
                                          softWrap: true,
                                          maxLines: 3,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),

                        Container(
                          height: 90,
                          decoration: BoxDecoration(
                              border: Border(
                                  bottom: BorderSide(
                                      color: AppColor.dividerColor, width: 2))),
                          padding: const EdgeInsets.fromLTRB(15, 0, 15, 0),
//          margin: const EdgeInsets.fromLTRB(0, 10, 0, 10),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: List.generate(
                                list.length,
                                (index) => Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Text(list[index]['title']),
                                        Text(list[index]['result']),
                                      ],
                                    )),
                          ),
                        ),

                        Container(
                          decoration: BoxDecoration(
                              border: Border(
                                  bottom: BorderSide(
                                      color: AppColor.dividerColor, width: 2))),
                          height: 70,
                          padding: EdgeInsets.fromLTRB(15, 0, 15, 00),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceAround,
                            children: [
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text('盛点抵扣'),
                                  Text(datas['miles'].toString()),
                                ],
                              ),
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text('应付'),
                                  Text(
                                    datas['money'],
                                    style: TextStyle(color: Colors.red),
                                  ),
                                ],
                              ),
                            ],
                          ),
                        ),

                        Container(
                            padding: EdgeInsets.fromLTRB(15, 0, 15, 0),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: [
                                Offstage(
                                  offstage: false,
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      Container(
                                        alignment: Alignment(0, 0),
                                        height: 35,
                                        child: Text(
                                            '邀请编码：${datas['coupon_code']}'),
                                      ),
                                      GestureDetector(
                                        onTap: () {
                                          print('点我了');
                                          showDialog<Null>(
                                            context: context,
                                            builder: (BuildContext context) {
                                              ///TODO: 这里的复制功能还没完成.
                                              return new SimpleDialog(
                                                title: new Text('复制成功'),
                                                children: [],
                                              );
                                            },
                                          ).then((val) {
                                            print(val);
                                          });
                                        },
                                        child: Text('复制邀请码'),
                                      ),
                                    ],
                                  ),
                                ),
                                Container(
                                  alignment: Alignment(-1, 0),
                                  height: 35,
                                  child: Text('订单时间： ${datas['create_time']}'),
                                ),
                              ],
                            )),

                        Expanded(
                          child: Container(color: AppColor.dividerColor),
                        ),
                      ],
                    );
            }),
      ),
    );
  }
}
